import { Injectable } from '@nestjs/common';
import {
  getWalletETH,
  getWalletNFTCountByToken,
  getWalletNFTs,
  getWalletWETH,
  sleep,
} from './utils/moralis-tools';
import { getFloorPrice } from './utils/opensea-tools';
import { getCache, setCache } from './utils/redis-tools';

@Injectable()
export class AppService {
  getHello(): string {
    return 'Hello World!';
  }
  async isWhale(address, chain = 'eth') {
    let totolETH = 0;
    const nft_count_list = await getWalletNFTCountByToken(address, chain);
    const nft_count_arrs = [];
    let total = 0;
    Object.keys(nft_count_list).map((key) => {
      nft_count_arrs.push({
        count: nft_count_list[key],
        token_address: key,
      });
      total = total + Number(nft_count_list[key]);
    });
    for (let i = 0; i >= 0; i++) {
      if (!nft_count_arrs[i]) {
        break;
      }
      const price_cache = await getCache(
        nft_count_arrs[i]['token_address'] + ':v6',
      );
      if (price_cache) {
        nft_count_arrs[i]['floor_price'] = price_cache;
        totolETH += Number(price_cache) * Number(nft_count_arrs[i]['count']);
      } else {
        const price = await getFloorPrice(
          nft_count_arrs[i]['token_address'],
          chain,
        );
        // const price = 1;
        await setCache(
          nft_count_arrs[i]['token_address'] + ':v6',
          price.toString(),
          600,
        );
        nft_count_arrs[i]['floor_price'] = price;
        totolETH += Number(price) * Number(nft_count_arrs[i]['count']);
      }
      if (totolETH >= 50) {
        return {
          state: true,
          total: "50+",
          nft_count: total,
          nfts_price_detail: nft_count_arrs,
        };
      }
    }
    if (totolETH >= 50) {
      return {
        state: true,
        total: "50+",
        nft_count: total,
        nfts_price_detail: nft_count_arrs,
      };
    }
    return {
      state: false,
      total: String(totolETH),
      nft_count: total,
      nfts_price_detail: nft_count_arrs,
    };
  }
  getWalletETH(address): any {
    console.log(address);
    return getWalletETH(address);
  }
}
